﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Web;
using System.Web.Caching;
using System.Configuration;
using Channelvn.Cached.Common;

namespace BAL
{
    public class CacheController
    {
        public static void SaveToCacheDependency(string database, string tableName, string cacheName, object data)
        {
            //SqlCacheDependency sqlDep = new SqlCacheDependency(database, tableName);
            //HttpContext.Current.Cache.Insert(cacheName, data, sqlDep);
        }
        public static void SaveToCacheDependency(int minute, string cacheName, object data)
        {
            HttpContext.Current.Cache.Insert(cacheName, data, null, DateTime.Now.AddMinutes(minute), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Default, null); 
        }
        public static void SaveToCacheDependency(string database, string[] tableName, string cacheName, object data)
        {
            AggregateCacheDependency aggCacheDep = new AggregateCacheDependency();
            SqlCacheDependency[] sqlDepGroup = new SqlCacheDependency[tableName.Length];
            for (int i = 0; i < tableName.Length; i++)
            {
                sqlDepGroup[i] = new SqlCacheDependency(database, tableName[i]);
            }
            aggCacheDep.Add(sqlDepGroup);
            HttpContext.Current.Cache.Insert(cacheName, data, aggCacheDep);
        }
        public static T GetFromCacheDependency<T>(string cacheName)
        {
            //object data = HttpContext.Current.Cache[cacheName];
            //if (null != data)
            //{
            //    try
            //    {
            //        return 
            //    }(T)data;
            //    catch
            //    {
            //        return default(T);
            //    }
            //}
            //else
            //{
                return default(T);
            //}
        }

        public static T GetRateCommentFromDistributedCache<T>(string cacheName)
        {
            if (Channelvn.Cached.CacheController.IsAllowDistributedCached)
            {
                return Channelvn.Cached.CacheController.Get<T>(Channelvn.Cached.Common.Constants.RATE_COMMENT_DATA_CACHE, cacheName);
            }
            else
            {
                return default(T);
            }
        }

        public static T GetADVFromDistributedCache<T>(string cacheName)
        {
            if (Channelvn.Cached.CacheController.IsAllowDistributedCached)
            {
                return Channelvn.Cached.CacheController.Get<T>(Channelvn.Cached.Common.Constants.ADV_DATA_CACHE, cacheName);
            }
            else
            {
                return default(T);
            }
        }

        public static T GetFromDistributedCacheNoLog<T>(string cacheName)
        {
            if (Channelvn.Cached.CacheController.IsAllowDistributedCached)
            {
                object obj = Channelvn.Cached.CacheController.Get<T>(Channelvn.Cached.Common.Constants.PARENT_CATEGORY_FOR_DATA_CACHE, cacheName);
                return (T)obj;
            }
            else
            {
                return default(T);
            }
        }
        public static T GetFromDistributedCache<T>(string cacheName)
        {
            string WriteCacheLog = ConfigurationManager.AppSettings["WriteCacheLog"];
            if (WriteCacheLog != null && WriteCacheLog.ToUpper() == "TRUE")
            {
                Utils.CreateLogFiles Err = new Utils.CreateLogFiles();
                Err.CacheLog(System.Web.Hosting.HostingEnvironment.MapPath("/ErrorLog"), Environment.NewLine + cacheName);
            }
            if (Channelvn.Cached.CacheController.IsAllowDistributedCached)
            {
                object obj = Channelvn.Cached.CacheController.Get<T>(Channelvn.Cached.Common.Constants.PARENT_CATEGORY_FOR_DATA_CACHE, cacheName);
                if (obj == null)
                {
                    string WriteNotInCacheLog = ConfigurationManager.AppSettings["WriteNotInCacheLog"];
                    if (WriteNotInCacheLog != null && WriteNotInCacheLog.ToUpper() == "TRUE")
                    {
                        Utils.CreateLogFiles Err = new Utils.CreateLogFiles();
                        Err.NotInCacheLog(System.Web.Hosting.HostingEnvironment.MapPath("/ErrorLog"), Environment.NewLine + cacheName);
                    }
                }
                else
                {
                    string WriteNotInCacheLog = ConfigurationManager.AppSettings["WriteInCacheLog"];
                    if (WriteNotInCacheLog != null && WriteNotInCacheLog.ToUpper() == "TRUE")
                    {
                        Utils.CreateLogFiles Err = new Utils.CreateLogFiles();
                        Err.InCacheLog(System.Web.Hosting.HostingEnvironment.MapPath("/ErrorLog"), Environment.NewLine + cacheName);
                    }
                }
                return (T)obj;
            }
            else
            {
                return default(T);
            }
        }

        public static void Add(string parentCategory, string key, object value, long timeExpire)
        {
            if (Channelvn.Cached.CacheController.IsAllowDistributedCached)
            {
                Channelvn.Cached.CacheController.Add(parentCategory, key, value, timeExpire);
            }
        }
    }
}
